﻿var trunPageObj;//翻页组件对像
var fromEleIds = "#confValue,.enableFlag,#note";
$(initPage);
function initPage() {
	
	//修改
	$("#btnEdit").click(function() {
		var confValue = $("#confValue").val();
		$.post("m/accrual_conf/cfgtype", {
			confType: $("#confType").val()
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				var fileList = "<select id='confValue' class='select-one select-width'>";
				if(data.submitConfs){
					var dataList = data.submitConfs;
					$(dataList).each(function(i) {
						fileList += "<option value='" + dataList[i].code + "' " + (dataList[i].code == confValue ? "selected" : "") + ">" + dataList[i].name + "</option>";
					});
				}
				fileList += "</select>";
				$("#confValueTD").html(fileList);
			} 
		}, "json");
		
		$("#btnEdit").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled",false);
		$(fromEleIds).attr("disabled",false);
	});
	
	//取消
	$("#btnCancle").click(function() {
		$("#btnEdit,#btnDelete,#btnSave,#btnCancle").attr("disabled", true);
		$("#btnAdd").attr("disabled",false);
		$(fromEleIds).attr("disabled", true);
		clearValues();
	});
	
	//保存
	$("#btnSave").click(function() {
		saveInfo();
	});
	
	//初始化翻页组件
    trunPageObj =  $(".pageNext").turnPage();
	trunPageObj.gotoPage(1);//搜索第一页
}

//清除表单值
function clearValues() {
	$("#busiName,#note").val("");
	$(".enableFlag").attr("checked", false);
	$("#confValueTD").html("<select id='confValue' class='select-one select-width' disabled><option value=''>　　　</option></select>");
	$("tr","#data_list").attr("bgColor","#FFFFFF");
}

//保存
function saveInfo() {
	var enableFlag = $.trim($(".enableFlag[checked]").val());
	$.post("m/accrual_conf/modify", {
			busiId:$.trim($("#busiId").val()),
			confValue:$.trim($("#confValue").val()),
			enableFlag:enableFlag == "true" ? "1":"0",
			note:$.trim($("#note").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：保存成功！");
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}

function queryList() {
	var pageno=trunPageObj.getCurrentPageNo();//当前第几页
	var pagecount=trunPageObj.getPageCount();//一页多少条
	$.post("m/accrual_conf/query", {
		pageNo: pageno,
		pageSize : pagecount
	}, function(data) {
		if ($.checkErrorMsg(data) ) {
			var fileList = "";
			if(data.businessConfPage && data.businessConfPage.dataList){
				var dataList = data.businessConfPage.dataList;
				$(dataList).each(function(i) {
					fileList += "<tr class='table_td_font' bgColor='#ffffff' onclick='selectTR(this)' busiid='" + dataList[i].busiId + "' enableflag='" + dataList[i].enableFlag + "'>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + (i + 1) + "</td>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].busiName + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + (dataList[i].enableFlag ? "启用" : "禁用") + "</td>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap conftype='" + dataList[i].confType + "' confvalue='" + dataList[i].confValue + "'>" + (dataList[i].enableFlag ? dataList[i].confValueText : "") + "</td>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap>" + (dataList[i].note ? dataList[i].note : "") + "</td>";
					fileList += "</tr>";
				});
			}
			$("#data_list").html(fileList);
			trunPageObj.setPageData(data.businessConfPage);
		} 
	}, "json");
}

function selectTR(obj) {
	$(obj).attr("bgColor","#FAEBD7").siblings().attr("bgColor","#FFFFFF");
	$("#busiId").val($(obj).attr("busiid"));
	$(".enableFlag").each(function(){
		if ($(this).val() == $(obj).attr("enableflag")) {
			$(this).attr("checked", true);
		}
	});
	var tds = $(obj).find("td");
	$("#busiName").val(tds.eq(1).text());
	$("#confType").val(tds.eq(3).attr("conftype"));
	$("#note").val(tds.eq(4).text());
	$("#confValueTD").html("<select id='confValue' class='select-one select-width' disabled><option value='" + tds.eq(3).attr("confValue") + "'>" + tds.eq(3).text() + "</option></select>");
	$("#btnSave,#btnCancle").attr("disabled", true);
	$("#btnEdit").attr("disabled",false);
	$(fromEleIds).attr("disabled", true);
}

function  sortObj(nodeId, flag) {
	$.post("m/submit_flow/sort", {
			nodeId:nodeId,
			sortUp:flag
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				$("#btnCancle").click();
				$("#data_list").html("");
				queryList();
			} 
		}, "json");
	event.cancelBubble=true;
}